%{
#define _BSD_SOURCE

//#include <string.h>
//#include <stdlib.h>

#define YY_EXTRA_TYPE struct T *

//#define ECHO yyextra->y = yytext; while(*yyextra->y) *(yyextra->b++) = *(yyextra->y++); *(yyextra->b) = 0;


struct T {
	char *content_type;
};

//%x dqstring qstring block_comment

%}

%option prefix="filemime_yy"
%option 8bit
%option extra-type="struct T *"
%option noyywrap
%option reentrant


%%

.*".txt"        yyextra->content_type = "text/plain; charset=UTF-8";
.*".html"       yyextra->content_type = "text/html; charset=UTF-8";
.*".css"        yyextra->content_type = "text/css; charset=UTF-8";
.*".js"         yyextra->content_type = "text/javascript; charset=UTF-8";
.*".jpg"        yyextra->content_type = "image/jpeg";
.*".png"        yyextra->content_type = "image/png";
.*".ico"        yyextra->content_type = "image/ico";
.               yyextra->content_type = NULL; yyterminate();

%%


char * get_file_content_type(char *filename) {
	//char *src = (char *)malloc(strlen(filename) + 1);
	//sprintf(src, "%s\n", 
	//printf("******* filename: %s\n", filename);
	struct T *x = (struct T *)malloc(sizeof(struct T));
	//x->content_type = "application/octet-stream";
	x->content_type = NULL;
	yyscan_t scanner;
	yylex_init(&scanner);
	yyset_extra(x, scanner);
	YY_BUFFER_STATE s = yy_scan_string(filename, scanner);
	yy_switch_to_buffer(s, scanner);
	yylex(scanner);
	yylex_destroy(scanner);
	return x->content_type;
}



